package com.oversea.action.useraction;

import static com.oversea.util.ParamUtil.isNotEmpty;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.User;
import com.oversea.models.UserAction;
import com.oversea.service.UserActionService;
import com.oversea.service.UserService;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/useractions")
@Permission(level = 3)
public class UserActionsAction extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	private UserService userService;

	private UserActionService userActionService;

	private List<UserAction> userActionsList;

	private User user;

	private UserAction userAction;

	private Page page;

	private int objId;

	private String loginTimeStart; // 登录时间
	private String loginTimeEnd;   // 登录时间
	private String logoutTime; // 登出时间
	private String serarchKeyWords; // 搜索关键字
	private String loginIp; // 登录IP

	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return UserAction.class;
	}

	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(loginTimeStart)) {
			try {
				searchContext.addOption(new SearchOption("loginTime", dateFormat.parse(loginTimeStart+" 00:00:00"),
						SearchOption.Option.bigthanandequal));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (isNotEmpty(loginTimeEnd)) {
			try {
				searchContext.addOption(new SearchOption("loginTime", dateFormat.parse(loginTimeEnd+" 00:00:00"),
						SearchOption.Option.smallthanandeqaul));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (isNotEmpty(serarchKeyWords)) {
			searchContext.addOption(new SearchOption("serarchKeyWords", "%"+serarchKeyWords+"%",
					SearchOption.Option.like));
		}
		if (isNotEmpty(loginIp)) {
			searchContext.addOption(new SearchOption("loginIp", loginIp,
					SearchOption.Option.eqaul));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/useractions/useractions_list.ftl") })
	public String list() {
		page = PageUtil.createPage(pageSize, currentPage, userActionService
				.getObjectCount(getListSearchContext()).intValue());
		userActionsList = userActionService.getObjectList(
				getListSearchContext(), pageSize, currentPage);
		return SUCCESS;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserActionService getUserActionService() {
		return userActionService;
	}

	public void setUserActionService(UserActionService userActionService) {
		this.userActionService = userActionService;
	}

	public List<UserAction> getUserActionsList() {
		return userActionsList;
	}

	public void setUserActionsList(List<UserAction> userActionsList) {
		this.userActionsList = userActionsList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserAction getUserAction() {
		return userAction;
	}

	public void setUserAction(UserAction userAction) {
		this.userAction = userAction;
	}

	public String getLoginTimeStart() {
		return loginTimeStart;
	}

	public void setLoginTimeStart(String loginTimeStart) {
		this.loginTimeStart = loginTimeStart;
	}

	public String getLoginTimeEnd() {
		return loginTimeEnd;
	}

	public void setLoginTimeEnd(String loginTimeEnd) {
		this.loginTimeEnd = loginTimeEnd;
	}

	public String getLogoutTime() {
		return logoutTime;
	}

	public void setLogoutTime(String logoutTime) {
		this.logoutTime = logoutTime;
	}

	public String getSerarchKeyWords() {
		return serarchKeyWords;
	}

	public void setSerarchKeyWords(String serarchKeyWords) {
		this.serarchKeyWords = serarchKeyWords;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	public SimpleDateFormat getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(SimpleDateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}
}
